﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using Foothill.Common.Logging;
using Foothill.DataAccess.Interface;

namespace Foothill.DataAccess.Repository
{
    public class VehicleInfoRepo : DbAccessBase, IVehicleInfoRepo
    {
        public IDictionary<int, string> GetVehicleMakes()
        {
            IDictionary<int, string> result = new Dictionary<int, string>();

            string sql = "SELECT [MakeId],[MakeName] FROM [Make]";
            DbCommand dbCommand = FoothillDB.GetSqlStringCommand(sql);

            try
            {
                using (IDataReader reader = FoothillDB.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        result.Add(reader.GetInt32(0), reader.GetString(1));
                    }
                }
            }
            catch (Exception exception)
            {
                Log.Error(FoothillModule.DataAccess, exception.Message, exception);
                throw new DataAccessException("Error when getting all the makes", exception);
            }

            return result;
        }

        public IDictionary<int, string> GetVehicleModels(int makeId)
        {
            IDictionary<int, string> result = new Dictionary<int, string>();

            string sql = "SELECT [ModelId],[ModelName] FROM [Model] WHERE [MakeId] = @makeid";
            DbCommand dbCommand = FoothillDB.GetSqlStringCommand(sql);
            FoothillDB.AddInParameter(dbCommand, "makeid", DbType.Int32, makeId);

            try
            {
                using (IDataReader reader = FoothillDB.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        result.Add(reader.GetInt32(0), reader.GetString(1));
                    }
                }
            }
            catch (Exception exception)
            {
                Log.Error(FoothillModule.DataAccess, exception.Message, exception);
                throw new DataAccessException(string.Format("Error when getting models for make id ={0}", makeId), exception);
            }

            return result;
        }

        public IDictionary<int, string> GetTransmissionTypes()
        {
            IDictionary<int, string> result = new Dictionary<int, string>();
            string sql = "SELECT [TransmissionTypeId],[TransmissionTypeName] FROM [TransmissionType]";
            DbCommand dbCommand = FoothillDB.GetSqlStringCommand(sql);

            try
            {
                using (IDataReader reader = FoothillDB.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        result.Add(reader.GetInt32(0), reader.GetString(1));
                    }
                }
            }
            catch (Exception exception)
            {
                Log.Error(FoothillModule.DataAccess, exception.Message, exception);
                throw new DataAccessException("Error when getting transmission types", exception);
            }

            return result;
        }

        public IDictionary<int, string> GetVehicleConditions()
        {
            IDictionary<int, string> result = new Dictionary<int, string>();
            string sql = "SELECT [VehicleConditionId],[VehicleConditionName] FROM [VehicleCondition]";
            DbCommand dbCommand = FoothillDB.GetSqlStringCommand(sql);

            try
            {
                using (IDataReader reader = FoothillDB.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        result.Add(reader.GetInt32(0), reader.GetString(1));
                    }
                }
            }
            catch (Exception exception)
            {
                Log.Error(FoothillModule.DataAccess, exception.Message, exception);
                throw new DataAccessException("Error when getting vehicle condition", exception);
            }

            return result;
        }

        public IDictionary<int, string> GetListingTypes()
        {
            IDictionary<int, string> result = new Dictionary<int, string>();
            string sql = "SELECT [ListingTypeId],[ListingTypeName] FROM [ListingType]";
            DbCommand dbCommand = FoothillDB.GetSqlStringCommand(sql);

            try
            {
                using (IDataReader reader = FoothillDB.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        result.Add(reader.GetInt32(0), reader.GetString(1));
                    }
                }
            }
            catch (Exception exception)
            {
                Log.Error(FoothillModule.DataAccess, exception.Message, exception);
                throw new DataAccessException("Error when getting listing types", exception);
            }

            return result;
        }
    }
}
